package com.hxzy.servlet.TbPanel;

import com.hxzy.entity.TbPanel;
import com.hxzy.service.TbPanelService;
import com.hxzy.service.impl.TbPanelServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(urlPatterns = "/TbPanel/Insert")
public class Insert extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/TbPanel/insert.jsp").forward(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        res.setCharacterEncoding("UTF-8");
        res.setContentType("text/html;charset=UTF-8");

        TbPanel tbPanel=new TbPanel();

        /**
         * 板块名称
         */
        String name=req.getParameter("name");
        tbPanel.setName(name);

        /**
         * 类型 0轮播图 1板块种类一 2板块种类二 3板块种类三
         */
        Integer type=Integer.parseInt(req.getParameter("type"));
        tbPanel.setType(type);

        /**
         * 排列序号
         */
        Integer sortOrder=Integer.parseInt(req.getParameter("sortOrder"));
        tbPanel.setSortOrder(sortOrder);


        /**
         * 所属位置 0首页 1商品推荐 2我要捐赠
         */
        Integer position=Integer.parseInt(req.getParameter("position"));
        tbPanel.setPosition(position);

        /**
         * 板块限制商品数量
         */
        Integer limitNum=Integer.parseInt("limitNum");
        tbPanel.setLimitNum(limitNum);

        /**
         * 状态
         */
        Integer status=Integer.parseInt("status");
        tbPanel.setStatus(status);

        /**
         * 备注
         */
        String remark=req.getParameter("remark");
        tbPanel.setRemark(remark);

        /**
         * 创建时间
         */
        SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD");
        try {
            Date created=sdf.parse(req.getParameter("created"));
            tbPanel.setCreated(created);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        /**
         * 更新时间
         */
        try {
            Date updated=sdf.parse(req.getParameter("updated"));
            tbPanel.setUpdated(updated);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        TbPanelService service=new TbPanelServiceImpl();
        boolean insert = service.insert(tbPanel);

//        PrintWriter out=res.getWriter();
//        out.println( insert? "新增成功": "新增失败");
//        //输出刷新
//        out.flush();
//        out.close();

        String path=req.getContextPath();
        req.setAttribute("url",path+"/TbPanel/search");
        //转发
        req.getRequestDispatcher("/WEB-INF/jump.jsp").forward(req,res);

    }
}
